package com.example.wsh.worddemo.Users_Fragment;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wsh.worddemo.DatabaseFile.AddWordToEveryDW;
import com.example.wsh.worddemo.DatabaseFile.AssetsDatabaseManager;
import com.example.wsh.worddemo.DatabaseFile.CursorGetAll;
import com.example.wsh.worddemo.R;
import com.hanks.htextview.base.HTextView;
import com.jaeger.library.StatusBarUtil;


import java.util.ArrayList;
import java.util.List;

import cn.addapp.pickers.widget.WheelListView;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class Setting extends AppCompatActivity {

    private Button sure;
    private TextView fuck;

    private String ciku;
    private String every_day_word;

    private Thread thread;

    private HTextView hTextView;

    private WheelListView wordnum;
    private List<String > list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        StatusBarUtil.setTranslucent(Setting.this);
        ActionBar actionBar  = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        ciku =prefs.getString("ciku",null);

        sure = findViewById(R.id.sure);
        wordnum = findViewById(R.id.select_num);
        fuck = findViewById(R.id.fuck);

        hTextView = findViewById(R.id.textview);
        hTextView.animateText("请选择每日单词量");

        for (int i=10;i <= 150;i=i+10)
        {
            list.add(String.valueOf(i));
        }
        wordnum.setItems(list);
        wordnum.setOnWheelChangeListener(new WheelListView.OnWheelChangeListener() {
            @Override
            public void onItemSelected(boolean b, int i, String s) {
                fuck.setText(s);
                every_day_word = fuck.getText().toString();
            }
        });

        final SweetAlertDialog sweetAlertDialogProgress = new SweetAlertDialog(Setting.this,SweetAlertDialog.PROGRESS_TYPE);
        sweetAlertDialogProgress.setTitleText("请稍等！");
        sweetAlertDialogProgress.setContentText("正在同步！");

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(Setting.this).edit();
                editor.putString("every_day_word",every_day_word);
                editor.apply();
                sweetAlertDialogProgress.show();
                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
                        SQLiteDatabase db = mg.getDatabase("Word.db");
                        //把完成了的放到AllDone表
                        String sqlDone = "Done is 1";
                        Cursor cursorDone = CursorGetAll.GetlimitedCursor("EveryDW",sqlDone);
                        while (cursorDone.moveToNext()){
                            ContentValues values = new ContentValues();
                            values.put("name",cursorDone.getString(cursorDone.getColumnIndex("name")));
                            db.insert("AllDone",null,values);
                            values.clear();
                        }
                        //还原没有完成的到原来的词库
                        String sqlNo = "Done is 0";
                        Cursor cursorNo = CursorGetAll.GetlimitedCursor("EveryDW",sqlNo);
                        String name;
                        while (cursorNo.moveToNext()){
                            name = cursorNo.getString(cursorNo.getColumnIndex("name"));
                            db.execSQL("update "+ciku+" set Selected = 0 where name is '"+name+"'");
                        }
                        //清空当前的EveryDW
                        db.execSQL("delete from EveryDW");
                        //添加新的单词到EveryDW
                        AddWordToEveryDW.Dosql(ciku,every_day_word);
                        try{
                            Thread.sleep(1500);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        sweetAlertDialogProgress.dismiss();
                        Looper.prepare();
                        Toast.makeText(Setting.this, "同步完成！", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                });
                thread.start();
            }
        });
    }
}
